<template>
  <div class="form">
    <el-button type="primary" @click="goback">返回</el-button>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm"
      size="small">
      <el-form-item label="科室名称" prop="DeptName">
        <el-input type="text" v-model="ruleForm.DeptName"></el-input>
      </el-form-item>
      <el-form-item label="科室编码" prop="DeptCode">
        <el-input type="text" v-model="ruleForm.DeptCode"></el-input>
      </el-form-item>
      <el-form-item label="科室分类" prop="DeptCategoryID">
        <el-input type="text" v-model="ruleForm.DeptCategoryID" autocomplete="new-password" clearable></el-input>
      </el-form-item>
      <el-form-item label="科室类别" prop="DeptType">
        <el-input type="text" v-model="ruleForm.DeptType"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addFun } from '@/CommonJs/RequestApi';
export default {
  name: "AddUser",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.Password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        DeptName: '',
        DeptCategoryID: '',
        DeptType: '',
      },
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    };
  },
  components: {

  }, methods: {


    submitForm(formName) {
      if (this.ruleForm.DeptName=='' && this.ruleForm.DeptCategoryID==''&&this.ruleForm.DeptType=='') {
        return alert('表单数据不能为空')
      } else {
        addFun('department/add_dept', this.ruleForm).then((response) => {
          if (response.data.state == 200) {
            alert('添加用户成功');
          } else {
            alert('用户添加失败')
          }
        })
      }


    },

    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    goback() {
      this.$router.push('/department')
    }
  }

}
</script>



<style scoped>
.form {
  width: 500px;
}
</style>